package com.knowledge.plus.other.类型转换;

import cn.hutool.core.convert.Convert;
import org.junit.Test;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * @author aFei
 * @since 2023/3/3
 */
public class TypeConvert {

    private Integer[] intArr = {1, 2, 3};
    private String[] strArr = new String[]{"1", "2", "3"};

    /**
     * 数组->List   Arrays.asList()
     */
    @Test
    public void toListTest() {
        List<Integer> intList = Arrays.asList(intArr);
        List<String> strList = Arrays.asList(strArr);
        System.out.println("数组转集合：" + intList);
    }

    /**
     * List->数组   list.toArray()
     */
    @Test
    public void toArraysTest() {
        List<Integer> intList = Arrays.asList(intArr);
        Integer[] intArray = intList.toArray(new Integer[5]);
        System.out.println("List<Integer>集合转数组：" + intArray.length);

        List<String> strList = Arrays.asList(strArr);
        String[] strArray = strList.toArray(new String[strArr.length]);
        System.out.println("List<String>集合转数组：" + strArray.length);
    }

    /**
     * hutool工具   Convert
     * {@code https://www.hutool.cn/docs }
     */
    @Test
    public void hutool() {
        // 数组转集合
        List<?> list1 = Convert.toList(intArr);
        List<?> list2 = Convert.toList(strArr);
        // 集合转数组
        Integer[] integers = Convert.toIntArray(list1);
        String[] strings = Convert.toStrArray(list2);
        System.out.println("List集合转数组：" + strings.length);

        // 日期字符串 -> 日期对象
        String dateStr = "2017-05-06";
        Date date = Convert.toDate(dateStr);
        System.out.println("字符串转日期对象：" + date);

        // 数字转换
        String f1 = Convert.numberToChinese(12653, true);
        System.out.println(f1);
    }

}
